/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.supplier;

import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import database.SupplierDAO;
import java.util.HashMap;
import java.util.List;
import model.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Supplier")
public class AddSupplierAction extends ActionSupport implements ModelDriven<Supplier>{
    private Supplier supplier = new Supplier();
    private List<Supplier> suppliers;
    public SupplierDAO supplierDAO = new SupplierDAO();

    public List<Supplier> getSuppliers() {
        return suppliers;
    }

    public void setSuppliers(List<Supplier> suppliers) {
        this.suppliers = suppliers;
    }

    
    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    
      @Action(value = "addsupplier", results = {
        @Result(name = "success", location = "/manageSupplier/w_supplier.jsp"),
        @Result(name = "input", location = "/manageSupplier/w_addsupplier.jsp")
    })
    @Override
    public String execute() throws Exception {
        String result;;
        System.out.println(supplier.getAddress());
        if (supplierDAO.InsertSupplier(supplier)) {
            addActionMessage("Add Supplier Successfully!");
            suppliers = supplierDAO.getAllSuppliers(); 
            result = SUCCESS;
        } else {
            addActionError("Add Failed (due to existed name).");
            result = INPUT;
        }

        return result;
    }
    @Override
    @VisitorFieldValidator(message = "", appendPrefix = false)
    public Supplier getModel() {
        return supplier; //To change body of generated methods, choose Tools | Templates.
    } 
}
